<?php
include("../conn.php");

// 需传参数
$userid = $_REQUEST[userid];
$goodsid = $_REQUEST[goodsid];
$shopid = $_REQUEST[shopid];
// 需传参数 end
if ($userid != '' && ($goodsid != '' || $shopid != '')) {
	$flag = true;
	if ($goodsid != '') {
		// 查询该用户是否已经收藏该商品
		$query = mysql_query("select * from my_collect where userid=".$userid." and goodsid=".$goodsid);
		$my_collect = mysql_fetch_assoc($query);
		// 查询该用户是否已经收藏该商品 end
		if (!$my_collect) {
			$query = mysql_query("INSERT INTO my_collect (userid,goodsid) VALUES (".$userid.",".$goodsid.")");
			if (mysql_affected_rows()<=0) {
				$flag = false1;
			}
			$query = mysql_query("UPDATE goods set collects=collects+1 where goodsid=".$goodsid);
			if (mysql_affected_rows()<=0) {
				$flag = false2;
			}
			if ($flag) {
				$arr = array('code' => 100, 'tip' => '收藏成功');
			}else{
				$arr = array('code' => 300, 'tip' => '收藏失败');
			}
		}else{
			$arr = array('code' => 400, 'tip' => '该用户已收藏该商品');
		}
	}elseif ($shopid != '') {
		// 查询该用户是否已经收藏该商家
		$query = mysql_query("select * from my_collect where userid=".$userid." and shopid=".$shopid);
		$my_collect = mysql_fetch_assoc($query);
		// 查询该用户是否已经收藏该商家 end
		if (!$my_collect) {
			$query = mysql_query("INSERT INTO my_collect (userid,shopid) VALUES (".$userid.",".$shopid.")");
			if (mysql_affected_rows()<=0) {
				$flag = false;
			}
			$query = mysql_query("UPDATE shop set collects=collects+1 where shopid=".$shopid);
			if (mysql_affected_rows()<=0) {
				$flag = false;
			}
			if ($flag) {
				$arr = array('code' => 100, 'tip' => '收藏成功');
			}else{
				$arr = array('code' => 300, 'tip' => '收藏失败');
			}
		}else{
			$arr = array('code' => 400, 'tip' => '该用户已收藏该商家');
		}
	}
}else{
	$arr = array('code' => 200, 'tip' => '参数传入有误,需传参数:用户id=>userid;商品id=>goodsid 或 商店id=>shopid');
}
echo json_encode($arr);